/**
 * All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
 * <p>
 * CbbPmDeliverableController 项目交付物
 * <p>
 * 修改纪录 2023-06-24 版本：1.0 weixinmin 创建。
 *
 * @version 版本：1.0
 * @author 作者：weixinmin 创建日期：2023-06-24
 */

package com.topscomm.mis.cbb.pm.controller;

import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.mis.cbb.pm.api.IDeliverableService;
import com.topscomm.mis.cbb.pm.api.ITaskService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmDeliverableEntity;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmProjectEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmDeliverableService;
import com.topscomm.mis.cbb.pm.util.PmSystemConst;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.TapSystemConst.TapOperationTypeEnum;
import com.topscomm.tap.constant.TapHttpErrorCode;
import com.topscomm.tap.constant.TapHttpStatus;
import groovy.util.logging.Log4j2;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.topscomm.mis.cbb.pm.controller.auto.CbbPmDeliverableControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: weixinmin
 * @date: 2023-06-24
 * @description:项目交付物Controller
 * @modify:
 */
@RestController
@RequestMapping("cbb/pmDeliverable")
public class CbbPmDeliverableController extends CbbPmDeliverableControllerAuto {

    @Autowired
    private IDeliverableService deliverableService;

    /**
     * @author: weixinmin
     * @date: 2023-06-24
     * @param paraMap
     * @return
     * @description:转换出查询SQL语句
     * @modify:
     */
    protected String spellListSql(Map<String, Object> paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" 1=1 ");
        if (paraMap.containsKey("qry_name")) {
            String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and name like '%{0}%' ", value);
			}
        }
        if (paraMap.containsKey("qry_code")) {
            String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and code = '{0}' ", value);
			}
        }
        // 项目号

		String value = paraMap.get(CbbPmDeliverableEntity.FieldProjectcode).toString();
		if (!StringUtil.isEmpty(value)) {
			sql.appendSingle(" and projectcode = '{0}' ", value);
		}

        // 管理员看所有，不控制权限
        // 项目经理看所有
        // 项目成员看公开的所有和自己提交的非公开的。
		CboUserEntity user = this.getUserBean().getUser();
		if (user.getAdminlevel() < PmSystemConst.ADMIN_LEVEL) {

			// 根据项目号获取当前项目的项目经理
			String pmCode = deliverableService.queryProjectByCode(value);
			// 项目经理不控制权限
			if (!user.getCode().equals(pmCode)){
				String dataPermission = deliverableService
					.getDataPermission(TapOperationTypeEnum.Select, CbbPmDeliverableEntity.tableName,
						this.getUserBean());
				// 默认权限只能看公开的和自己上传的
				if (StringUtil.isEmpty(dataPermission)) {
					sql.appendSingle(" and (createuser = {0} or open = 1)",
						user.getId());
				} else {
					// 使用数据权限
					sql.appendSingle(" and {0} ", dataPermission);
				}
			}
		}

        return sql.toString();
    }

    /**
     * description 列表页
     * @author wxm
     * @date 2023-06-25 17:28:20
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @modify
     */
    @GetMapping(value = "/list")
    public ResponseResult<Object> list(
        @RequestParam(required = false) Map<String, Object> paraMap) {
        try {
            BaseUtil.decodeSecureMap(paraMap);
            String sqlWhere = this.spellListSql(paraMap);
            Page page = deliverableService.list(paraMap, sqlWhere);
            return ResponseResult.ok(page);
        } catch (Exception ex) {
            return ResponseResult
                .error(TapHttpStatus.HTTP_INTERNAL_ERROR, TapHttpErrorCode.ERROR, ex.getMessage());
        }
    }

    /**
     * description 加载详情页
     * @author wxm
     * @date 2023-06-25 18:45:39
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @modify
     */
	@GetMapping(value = "/load")
	public ResponseResult<Object> load(@RequestParam(required = false) Map<String, Object> paraMap) {
		BaseUtil.decodeSecureMap(paraMap);
		Map<String, Object> dataMap = deliverableService.load(paraMap);
		return ResponseResult.ok(dataMap);
	}

    /**
     * description 插入数据
     * @author wxm
     * @date 2023-06-25 18:14:30
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @modify
     */
    @PostMapping(value = "/insert")
    public ResponseResult<Object> insert(
        @RequestBody(required = false) Map<String, Object> paraMap) {
        try {
            Map<String, Object> resMap = deliverableService.insert(paraMap);
            return ResponseResult.ok(resMap);
        } catch (Exception e) {
            return ResponseResult
                .error(TapHttpStatus.HTTP_INTERNAL_ERROR, TapHttpErrorCode.ERROR, e.getMessage());
        }

    }

    /**
     * description 更新
     * @author wxm
     * @date 2023-06-25 18:17:00
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @modify
     */
    @PutMapping(value = "/update")
    public ResponseResult<Object> update(
        @RequestBody(required = false) Map<String, Object> paraMap) {
    	try {
			deliverableService.update(paraMap);
			return ResponseResult.ok("修改成功");
		}catch (Exception e){
			return ResponseResult
				.error(TapHttpStatus.HTTP_INTERNAL_ERROR, TapHttpErrorCode.ERROR, e.getMessage());
		}
    }

    /**
     * description 删除
     * @author wxm
     * @date 2023-06-25 18:20:48
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @modify
     */
	@DeleteMapping(value = "/delete")
	public ResponseResult<Object> delete(@RequestBody(required = false) Map<String, Object> paraMap) {
		BaseUtil.decodeSecureMap(paraMap);
		deliverableService.delete(paraMap);
		return ResponseResult.ok("删除成功");
	}

}

